<template>
  <div class="content">
    <jc-button
      :btn-arr="['reload', bomCost('supp:save') && 'newy', 'remove', 'pass', 'sub', 'option']"
      :disab-arr="['close', 'cancel', 'copy', 'freeze', 'com1', 'com2', 'chart']"
      @refresh="handleGetUntreated"
      @newly="addMateril"
      @audit="Retrial(1)"
      @noAudit="Retrial(2)"
      @submit="materSub(1)"
      @noSubmit="materSub(2)"
      @forbidden="using(1)"
      @startUsing="using(2)"
      @del="delCustom"
      @toExcel="handleExport"
    />
    <div class="header">
      <div class="header-name">
        <search :options="selectData" :search-son="searchSon" :msg="tacitly" @seek="collect" @hand="handleQuerySonClass" />
        <el-button type="primary" class="btn" size="mini" @click="handleQuerySonClass">{{ $t('btn.search') }}</el-button>
        <filtration
          :ftype="ftype"
          :msg="tacitly"
          :select-data="selectData"
          :table-header="tableHeader1"
          @seachData="handleQuerySonClass"
        />
      </div>
    </div>
    <div class="table-content">
      <jc-table
        :table-data="tableData"
        :table-header="tableHeader"
        serial
        sortable
        table-selection
        get-row-keys="fsupplierId"
        @selectionChange="selectionDatas"
        @clickRow="particulars"
      >
        <template v-slot:tagSlot="col">
          <el-link type="primary" @click="particulars(col.scope.row)">{{ col.scope.row.fname }}</el-link>
        </template>
        <template v-slot:billSlot="clo">
          <el-tag v-if="clo.scope.row.fforbidStatus==='否'">{{ clo.scope.row.fforbidStatus }}</el-tag>
          <el-tag v-else type="danger">{{ clo.scope.row.fforbidStatus }}</el-tag>
        </template>
        <template v-slot:btnState="col">
          <el-tag :type="col.scope.row.stateColor">{{ col.scope.row.fdocumentStatus }}</el-tag>
        </template>
      </jc-table>
    </div>
    <!--    分页器-->
    <div class="footer">
      <jc-pagination
        v-show="total > 0"
        :total="total"
        :page.sync="currentPage"
        :limit.sync="size"
        @pagination="handleGetUntreated"
      />
    </div>
  </div>
</template>

<script>
import {
  querySupplierList,
  undisableSupplier,
  disableSupplier,
  supplierAuSubmit,
  supplierSubmit,
  supplierUnAudit,
  supplierAudit,
  deleteSupplier
} from '@/api/purchaseManagement/supplier'
import search from '@/components/Search'
import searData from '@/components/Search/mixin'
import { toMxAmina, Disable } from '@/components/ToMxamineState'
import { export_json_to_excel, formatJson } from '@/utils/Export2Excel'
import filtration from '@/components/filtration'
import jcButton from '@/components/Button'
export default {
  components: {
    search,
    filtration,
    jcButton
  },
  mixins: [searData],
  data() {
    return {
      ftype: 11,
      total: 0, // 总条目
      currentPage: 1, // 当前页
      size: 200, // 每页显示多少条数据
      // 表头
      tableData: [], // 销售数据
      tableHeader: [
        { label: this.$t('shop.fnumber'), prop: 'fnumber' },
        { label: this.$t('market.suppliername'), type: 'tag', prop: 'fname' },
        { label: this.$t('market.short'), prop: 'fshortname' },
        { label: this.$t('market.primaryGroup'), prop: 'fprimarygroupName' },
        { label: this.$t('market.approver'), prop: 'fapprover' },
        { label: this.$t('market.documentstatus'), type: 'state', prop: 'fdocumentStatus' },
        { label: this.$t('market.forbidStatu'), type: 'bill', prop: 'fforbidStatus' }
      ],
      tableHeader1: [
        { label: this.$t('market.descrip'), prop: 'fdescription' },
        { label: this.$t('market.creator'), prop: 'fcreator' },
        { label: this.$t('market.createDate'), prop: 'fcreateDate' },
        { label: this.$t('market.modifier'), prop: 'fmodifier', width: '80' },
        { label: this.$t('market.modifyDate'), prop: 'fmodifyDate' },
        { label: this.$t('market.approveDate'), prop: 'fapproveDate' },
        { label: this.$t('market.forbider'), prop: 'fforbider', width: '80' },
        { label: this.$t('market.forbidDate'), prop: 'fforbidDate' },
        { label: this.$t('market.contact'), prop: 'fcontact' },
        { label: this.$t('market.ftels'), prop: 'fcontacttel' },
        { label: this.$t('market.mailbox'), prop: 'fcontactemail' },
        { label: this.$t('market.country'), prop: 'fcountryName' },
        { label: this.$t('market.fzip'), prop: 'fzip' },
        { label: this.$t('market.language'), prop: 'flanguage' },
        { label: this.$t('market.addressBook'), prop: 'faddress' },
        { label: this.$t('market.website'), prop: 'fwebsite' },
        { label: this.$t('market.registeraddress'), prop: 'fregisteraddress' },
        { label: this.$t('market.trade'), prop: 'ftrade' },
        { label: this.$t('market.companynature'), prop: 'fcompanynatureName' },
        { label: this.$t('market.ffounddate'), prop: 'ffounddate' },
        { label: this.$t('market.legalperson'), prop: 'flegalperson' },
        { label: this.$t('market.registerfund'), prop: 'fregisterfund' },
        { label: this.$t('market.registercode'), prop: 'fregistercode' },
        { label: this.$t('market.tendpermit'), prop: 'ftendpermit' },
        { label: this.$t('market.staff'), prop: 'fstaff' },
        { label: this.$t('market.supplierclassify'), prop: 'fsupplierclassify' },
        { label: this.$t('market.supplyclassify'), prop: 'fsupplyclassifyName' },
        { label: this.$t('market.companyclassify'), prop: 'fcompanyclassifyName' },
        { label: this.$t('market.supplierscale'), prop: 'fcompanyscaleName' },
        { label: this.$t('market.socialcrecode'), prop: 'fsocialcrecode' },
        { label: this.$t('market.trade'), prop: 'ftradestring' },
        { label: this.$t('market.pricelist'), prop: 'fpricelistName' },
        { label: this.$t('market.settleCurr'), prop: 'fpaycurrencyName' },
        { label: this.$t('market.paymentClause'), prop: 'fpayconditionName' },
        { label: this.$t('market.taxType'), prop: 'ftaxtypeName' },
        { label: this.$t('market.taxrate'), prop: 'ftaxrateName' },
        { label: this.$t('market.invoiceType'), prop: 'finvoicetypeName' },
        { label: this.$t('market.taxregister'), prop: 'ftaxregistercode' },
        { label: this.$t('market.settleType'), prop: 'fpaymenttypeName' },
        { label: this.$t('market.tendtype'), prop: 'ftendtypeName' }
      ]
    }
  },
  created() {
    this.handleGetUntreated()
    this.tableHeader1 = this.tableHeader.concat(this.tableHeader1)
  },
  methods: {
    // 导出表格
    handleExport() {
      require.ensure([], () => {
        const tHeader = this.tableHeader.map((res) => {
          return res.label
        })
        const filterVal = this.tableHeader.map((res) => {
          return res.prop
        })
        if (this.tableData.length <= 0) {
          this.$message.warning('没有导出数据')
        } else if (this.val && this.val.length > 0) {
          const list = this.val
          const data = formatJson(filterVal, list)
          export_json_to_excel(tHeader, data, '供应商列表')
        } else {
          querySupplierList({
            pageSize: 100000,
            pageNum: 1,
            search: this.searCollData
          }).then(res => {
            if (res.code === 0) {
              const list = res.data.array.map(item => {
                return (toMxAmina(item), Disable(item))
              })
              const data = formatJson(filterVal, list)
              export_json_to_excel(tHeader, data, '供应商列表')
            } else {
              this.$confirm(res.message, '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'error'
              })
            }
          })
        }
      })
    },
    // 获取供应商列表数据
    async handleGetUntreated(ev) {
      this.details = []
      if (ev && !ev.page) {
        this.details = ev
      }
      const DATA = { pageNum: this.currentPage, pageSize: this.size, search: this.searCollData.concat(this.details) }
      const { data: RES } = await querySupplierList(DATA)
      this.tableData = RES.array.map(item => {
        return (toMxAmina(item), Disable(item))
      })
      this.total = RES.total
    },
    // 搜索
    handleQuerySonClass(ev) {
      this.currentPage = 1
      if (ev && ev.fname) {
        this.searchSon = ev.detail
        this.customHandle(ev)
        this.handleGetUntreated(ev.detail)
      } else {
        this.handleGetUntreated()
      }
    },
    selectionDatas(val) {
      this.selectFbi = []
      this.val = val
      this.val.forEach(res => {
        res.fnumber && this.selectFbi.push(res.fnumber)
      })
    },
    // 公共函数
    toExamineFun(api) {
      const DATA = { Numbers: this.selectFbi }
      api(DATA).then(res => {
        if (res.code !== 0) {
          this.$confirm(res.message, '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'error'
          })
          return
        }
        this.handleGetUntreated()
        this.selectFbi = []
        this.innerHTM = ''
        res.data.forEach(res => {
          this.innerHTM += '<div>' + res + '</div>'
        })
        this.$notify.warning({
          title: '提示',
          message: this.innerHTM,
          duration: 3000, offset: 50,
          dangerouslyUseHTMLString: true
        })
      })
    },
    // 审核
    Retrial(ev) {
      const api = ev === 1 ? supplierAudit : supplierUnAudit
      this.toExamineFun(api)
    },
    // 提交
    materSub(ev) {
      const api = ev === 1 ? supplierSubmit : supplierAuSubmit
      this.toExamineFun(api)
    },
    // 禁用
    using(ev) {
      const api = ev === 1 ? disableSupplier : undisableSupplier
      this.toExamineFun(api)
    },
    // 删除
    delCustom() {
      this.$confirm('确认要删除么？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'error'
      }).then(() => {
        const api = deleteSupplier
        this.toExamineFun(api)
      })
    },
    // 新增
    addMateril() {
      this.$router.push({ path: '/createSupplier' })
    },
    // 价目详情
    particulars(ev) {
      const id = ev.fsupplierId
      this.$router.push({ path: `/editSupplier/${id}` })
    },
    // 自定义列表
    customHandle(ev) {
      this.tableHeaders = []
      if (ev.header) {
        ev.header.forEach(item => {
          this.tableHeader1.forEach(res => {
            if (res.label === item) {
              this.tableHeaders.push(res)
            }
          })
        })
        this.tableHeader = this.tableHeaders
      } else if (ev.fname === '缺省方案') {
        this.tableHeader = this.oldTabHeader
      } else {
        this.tableHeader = this.tableHeader1
      }
    }
  }
}
</script>
<style lang="scss" scoped>
.content {
  @include listBom;
  .header{
    position:relative;
    .header-name{
      width: 100%;
    }
    .btn{
      transform: translateY(18%);
      margin-left: 450px !important;
    }
  }
}
</style>
